/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2025-06-02 03:12:38
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-07-31 20:33:57
 */
#include "public.h"

#include "power.h"

#include "led.h"
#include "key.h"
#include "task.h"

#include "stdio.h"

void dev_init(void)
{
    if (pstDev == NULL)
    {
        pstDev = (struct _dev *)malloc(sizeof(struct _dev));
    }
    if (pstDev == NULL)
    {
        printf("malloc err\r\n");
        return;
    }

    pstDev->pstRecvMessage = (struct _message_fram *)malloc(sizeof(struct _message_fram));

    power_init();

    led_init();

    key_init();

    canTxMessagePackInit();

    loop_task_init();
    delay_task_init();
    

    pstDev->stDevDyp1.bIsMeasEndFlag = false;
    pstDev->stDevDyp2.bIsMeasEndFlag = false;
    pstDev->stDevDyp3.bIsMeasEndFlag = false;

    pstDev->stDevFall.bIsFall1 = false;
    pstDev->stDevFall.bIsFall2 = false;
    pstDev->stDevFall.bIsFall3 = false;
    pstDev->stDevFall.bIsFall4 = false;

    pstDev->bIsGetGTMessage = false;
    pstDev->bIsCharging = false;
    pstDev->bIsOutPowerCtrlPerm = false;
    pstDev->ucPowerStat = 0;

    pstDev->stDevMotor.bIsStop = false;//上电不急停
		
		pstDev->bIsRecvCanMessageFlag = false;

    pstDev->uiSysMsTime = 0;
    pstDev->uiDypRunMsTime = 0;
    pstDev->uiIcmRunMsTime = 0;
    pstDev->uiBatChargMsTime = 0;
    pstDev->uiStepMsTime = 0;
    pstDev->uiLedMsTime = 0;


    // pstDev->uiSysStartTime = 0;
	// pstDev->ucSysShutDownTime = 0;

    HAL_Delay(10);
    //   uint8_t icm_id = ICM42605_ReadReg(ICM42605_WHO_AM_I);

    // delay_task_test();
}

void magnetCtrl(uint8_t ucOnOffFlag)
{
    if(ucOnOffFlag == 1)//电磁铁使能
    {
        HAL_GPIO_WritePin(MAGNET_GPIO_Port,MAGNET_Pin,1);
    }
    else if(ucOnOffFlag == 0)//电磁铁使能
    {
        HAL_GPIO_WritePin(MAGNET_GPIO_Port,MAGNET_Pin,0);
    }
}